package com.simple.weibo.database;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.simple.weibo.bean.User;

public class SimpleDatabase {

    private SQLiteOpenHelper helper;
    private SQLiteDatabase db;

    public SimpleDatabase(SQLiteOpenHelper helper) {
        this.helper = helper;
        db = helper.getWritableDatabase();
    }

    /**
     * 获取所有用户列表
     *
     * @return
     */
    public List<User> getCountList() {

        List<User> userList = new ArrayList<User>();
        Cursor cursor = db.query(User.TABLE_NAME, null, null, null, null, null,
                null);
        if (cursor.moveToFirst()) {
            do {
                User user = new User();
                user.setId(cursor.getInt(cursor.getColumnIndex(User.ID)));

                user.setUserId(cursor.getString(cursor.getColumnIndex(User.USER_ID)));
                user.setAccessToken(cursor.getString(cursor.getColumnIndex(User.ACCESS_TOKEN)));
                user.setAccessTokenSecret(cursor.getString(cursor.getColumnIndex(User.ACCESS_TOKEN_SECRET)));

                byte[] b = cursor.getBlob(cursor.getColumnIndex(User.ICON));
                if (b != null) {
                    ByteArrayInputStream bis = new ByteArrayInputStream(b);
                    Drawable draw = Drawable.createFromStream(bis, "image");
                    user.setIcon(draw);
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                String name = cursor.getString(cursor.getColumnIndex(User.USER_NAME));
                if (name != null) {
                    user.setUserName(name);
                }
                userList.add(user);

            } while (cursor.moveToNext());
        }
        cursor.close();

        return userList;

    }

    /**
     * 新增用户授权信息
     *
     * @param user
     */
    public void addUser(User user) {
        ContentValues values = new ContentValues();
        values.put(User.USER_ID, user.getUserId());
        values.put(User.ACCESS_TOKEN, user.getAccessToken());
        values.put(User.ACCESS_TOKEN_SECRET, user.getAccessTokenSecret());

        db.insert(User.TABLE_NAME, null, values);
    }

    /**
     * 更新用户的昵称和头像
     *
     * @param user
     */
    public void updateInfo(User user) {
        ContentValues values = new ContentValues();
        values.put(User.USER_NAME, user.getUserName());
        BitmapDrawable bd = (BitmapDrawable) user.getIcon();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bd.getBitmap().compress(CompressFormat.PNG, 100, bos);
        values.put(User.ICON, bos.toByteArray());

        db.update(User.TABLE_NAME, values, User.USER_ID + "=" + user.getUserId(), null);
    }

    /**
     * 删除用户授权信息
     *
     * @param userId
     */
    public void deleteUser(String userId) {
        db.delete(User.TABLE_NAME, User.USER_ID + "=" + userId, null);
    }

    /**
     *根据userId获取User信息
     * @param userId
     * @return
     */
    public User getUserById(String userId) {
        User user = new User();
        Cursor cursor = db.query(User.TABLE_NAME, null, User.USER_ID + "=" + userId, null, null, null, null);
        if (cursor.moveToFirst()) {
            user.setId(cursor.getInt(cursor.getColumnIndex(User.ID)));

            user.setUserId(cursor.getString(cursor.getColumnIndex(User.USER_ID)));
            user.setAccessToken(cursor.getString(cursor.getColumnIndex(User.ACCESS_TOKEN)));
            user.setAccessTokenSecret(cursor.getString(cursor.getColumnIndex(User.ACCESS_TOKEN_SECRET)));

            byte[] b = cursor.getBlob(cursor.getColumnIndex(User.ICON));
            if (b != null) {
                ByteArrayInputStream bis = new ByteArrayInputStream(b);
                Drawable draw = Drawable.createFromStream(bis, "image");
                user.setIcon(draw);
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            String name = cursor.getString(cursor.getColumnIndex(User.USER_NAME));
            if (name != null) {
                user.setUserName(name);
            }
        }
        cursor.close();
        return user;

    }

    public void close() {
        helper.close();
    }

}
